package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorPanoramaGetDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorUserAndDeptGetDTO;
import com.xbongbong.pro.competitor.pojo.dto.LinkedOpportunityListDTO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorPanoramaGetVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorUserAndDeptGetVO;
import com.xbongbong.pro.competitor.pojo.vo.LinkedOpportunityListVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.listbatch.pojo.dto.PortionDepDTO;
import com.xbongbong.pro.listbatch.pojo.dto.PortionUserDTO;
import com.xbongbong.saas.service.CompetitorService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/8/22 15:45
 * @description: 竞争对手
 */
@SuppressWarnings("deprecation")
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/competitor")
public class CompetitorController {
    @Resource
    private CompetitorService competitorService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 竞争对手详情tab获取全景分析
     * date: 2022/8/23 7:04 下午
     * @author yingjf
     * @param competitorPanoramaGetDTO:
     * @param br:
     */
    @RequestMapping(value = "/panorama/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formGet(@RequestBody @Valid CompetitorPanoramaGetDTO competitorPanoramaGetDTO, BindingResult br){
        XbbResponse<CompetitorPanoramaGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorPanoramaGetVO competitorPanoramaGetVO = competitorService.panoramaGet(competitorPanoramaGetDTO);
                response = new XbbResponse<>(competitorPanoramaGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手详情tab获取可见范围
     * date: 2022/8/23 5:03 下午
     * @author yingjf
     * @param competitorUserAndDeptGetDTO:
     * @param br:
     */
    @RequestMapping(value = "/userAndDept/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userAndDeptGet(@RequestBody @Valid CompetitorUserAndDeptGetDTO competitorUserAndDeptGetDTO, BindingResult br) {
        XbbResponse<CompetitorUserAndDeptGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorUserAndDeptGetVO competitorUserAndDeptGetVO = competitorService.userAndDeptGet(competitorUserAndDeptGetDTO);
                response = new XbbResponse<>(competitorUserAndDeptGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 竞争对手详情tab增加部门
     * date: 2022/8/23 7:04 下午
     * @author yingjf
     * @param portionDepDTO:
     * @param br:
     */
    @RequestMapping(value = "/addDept", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String portionDep(@RequestBody @Valid PortionDepDTO portionDepDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(portionDepDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(portionDepDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = competitorService.portionDep(portionDepDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 竞争对手详情tab增加用户
     * date: 2022/8/23 7:05 下午
     * @author yingjf
     * @param portionUserDTO:
     * @param br:
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String portionUser(@RequestBody @Valid PortionUserDTO portionUserDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(portionUserDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(portionUserDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = competitorService.portionUser(portionUserDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手tab查询关联销售机会列表
     * date: 2022/8/23 8:34 下午
     * @author yingjf
     * @param linkedOpportunityListDTO:
     * @param br:
     */
    @RequestMapping(value = "/linkedOpportunity/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String portionUser(@RequestBody @Valid LinkedOpportunityListDTO linkedOpportunityListDTO, BindingResult br) {
        XbbResponse<LinkedOpportunityListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkedOpportunityListVO linkedOpportunityListVO = competitorService.linkedOpportunityList(linkedOpportunityListDTO);
                response = new XbbResponse<>(linkedOpportunityListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}






